package com.fw.ui.view.settings.usercontrol.parentmerchant.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.merchantprofile.bean.MerchantProfileBean;
import com.fw.service.merchantprofile.bean.SearchMerchantProfileBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.MerchantProfileBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.bean.parentmerchant.SearchMerchantProfileTableItemBean;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.common.PagedTable;
import com.fw.ui.view.common.TablePanel;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.CreateParentMerchantView;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class CreateParentMerchantSearchMerchantProfileSearchButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MerchantProfileBAO merchantProfileBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("rawtypes")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	CreateParentMerchantView createParentMerchantView = (CreateParentMerchantView) this.getBasedComponent();

	TablePanel tableMerchantPanel = createParentMerchantView.getTableMerchantPanel();

	Integer size = tableMerchantPanel.getManualPageSize();
	int page = 1;

	SearchCriteriaBean<SearchMerchantProfileBean> searchCriteriaBean = createParentMerchantView.getSearchCriteriaBean();
	SearchMerchantProfileBean criteriaBean = searchCriteriaBean.getCriteriaBean();
	criteriaBean.setMerchantId(createParentMerchantView.getTxtMerchantID().getValue());
	criteriaBean.setCompanyName(createParentMerchantView.getTxtMerchantCompanyName().getValue());
	criteriaBean.setMerchantUrl(createParentMerchantView.getTxtMerchantUrl().getValue());
	criteriaBean.setProjectContactPerson(createParentMerchantView.getTxtMerchantContactPerson().getValue());
	criteriaBean.setStatus(createParentMerchantView.getCbbMerchantStatus().getValue().toString());
	criteriaBean.setSearchAllStatus(Boolean.TRUE);

	TablePanel tableMerchantSelectedPanel = createParentMerchantView.getTableMerchantSelectedPanel();
	PagedTable pagedSelectedTable = tableMerchantSelectedPanel.getTable();
	List<Long> excludeMerchantIds = new ArrayList<Long>();
	for (Object itemId : pagedSelectedTable.getItemIds()) {
	    SearchMerchantProfileTableItemBean searchMerchantProfileTableItemBean = (SearchMerchantProfileTableItemBean) itemId;
	    excludeMerchantIds.add(searchMerchantProfileTableItemBean.getId());
	}
	criteriaBean.setExcludeMerchantIds(excludeMerchantIds);

	ApplicationUI applicationUI = (ApplicationUI) createParentMerchantView.getUI();
	sessionInfoUtils.setSessionInfoDetails(criteriaBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	String sortingDefault = this.propertyBAO
		.getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.CreateParentMerchantView.default.table.sorting");
	searchCriteriaBean.setDirection1(SearchCriteriaBean.DIRECTION_ASC);
	searchCriteriaBean.setOrder1(sortingDefault);
	searchCriteriaBean.setCriteriaBean(criteriaBean);
	searchCriteriaBean.setPage(page);
	searchCriteriaBean.setSize(size);

	SearchResultBean<MerchantProfileBean> searchResultBean = merchantProfileBAO.selectMerchantProfile(searchCriteriaBean);

	Integer totalPage = searchResultBean.getTotalPages();
	if (totalPage == null) {
	    totalPage = 1;
	}
	Long totalElement = searchResultBean.getTotalElements();
	if (totalElement == null) {
	    totalElement = 0l;
	}
	List<BeanItem> beanItems = createParentMerchantView.generateTableItemBean(searchResultBean);

	createParentMerchantView.getTableMerchantPanel().clearTableData();
	createParentMerchantView.getTableMerchantPanel().setManualPageTable(1);
	createParentMerchantView.getTableMerchantPanel().setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);

	createParentMerchantView.getTableMerchantSelectedPanel().clearTableData();

	if (beanItems.isEmpty()) {
	    Map<String, Object> result = new HashMap<String, Object>();
	    result.put(MessageBAO.MESSAGE_INFO_TYPE, "message.search.not.found");
	    return result;
	}
	return null;
    }

    @Override
    public boolean validate(ClickEvent event) {
	return true;
    }

}
